/*
 * Copyright 2006 Robert Burrell Donkin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package rat.report.xml.visitor;

import java.io.IOException;

import rat.element.ArchiveElement;
import rat.element.BinaryElement;
import rat.element.Directory;
import rat.element.IElementVisitor;
import rat.element.NoteElement;
import rat.element.StandardContentElement;
import rat.report.ReportFailedRuntimeException;
import rat.report.xml.writer.IXmlWriter;

public final class ElementOpenVisitor implements IElementVisitor {

    private final IXmlWriter xmlWriter;
    
    public ElementOpenVisitor(IXmlWriter xmlWriter) {
        this.xmlWriter = xmlWriter;
    }
    
    public void visit(Directory directory) {
        openElement("directory");
    }

    public void visit(StandardContentElement standardContent) {
        openElement("resource");
        attribute("type", "standard");
    }

    public void visit(NoteElement note) {
        openElement("resource");
        attribute("type", "notice");
    }

    public void visit(BinaryElement binary) {
        openElement("resource");
        attribute("type", "binary");
    }

    public void visit(ArchiveElement archive) {
        openElement("resource");
        attribute("type", "archive");
    }
    
    private void openElement(String name) {
        try {
            xmlWriter.openElement(name);
        } catch (IOException e) {
            // Cannot recover
            throw new ReportFailedRuntimeException("'" + name + "' element creation failure", e);
        }
    }
    
    
    private void attribute(String name, String value) {
        try {
            xmlWriter.attribute(name, value);
        } catch (IOException e) {
            // Cannot recover
            throw new ReportFailedRuntimeException("'" + name + "' attribute creation failure", e);
        }
    }
}
